//
//  CommonTableViewController.m
//  GiftNote
//
//  Created by wsh on 15/8/14.
//  Copyright (c) 2015年 GN. All rights reserved.
//

#import "CommonTableViewController.h"
#import "PKRequestManager.h"
#import "HomeGiftCell.h"
#import "MJRefresh.h"
#import "Screen.h"
#import "INHeader.h"
#import "HomeBaseDetailViewController.h"
#import "MBProgressHUD+MJ.h"


//  用判断刷新的方式(是下拉刷新还是上拉加载)
typedef NS_ENUM(NSInteger, RefreashType){
    RefreashTypeNew,  //  下拉刷新
    RefreashTypeMore     //  上拉加载
};

@interface CommonTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataArray;  //  存储数据的数组

@property (nonatomic, assign) NSInteger num;  // 每次加载的数据个数
@property (nonatomic, assign) RefreashType refreashType; //  刷新的方式
@end


@implementation CommonTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //  设置标题
    self.navigationItem.title = _nameString;
    
    //  改变标题的颜色
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                [UIColor whiteColor],
                                NSForegroundColorAttributeName, nil];
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    
    //  改变返回按钮的颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    //  注册cell
    [self.tableView registerClass:[HomeGiftCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    
    //  设置一开始加载的数据个数以及调用刷新函数
    _num = 0;
    [self refreashData];
    
    //  设置背景图片
    UIImageView *backView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    backView.image = [UIImage imageNamed:@"beijing"];
    self.tableView.backgroundView = backView;
    
    
    //  刷新界面
    [self readData];
    
    
    [MBProgressHUD showMessage:@"正在加载数据....."];
}



#pragma mark --- 懒加载
- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
}



#pragma mark --- 解析数据
- (void)readData{

    [PKRequestManager manager:GET requestWith:[_urlString stringByAppendingString:[NSString stringWithFormat:@"%ld",(long)_num]] parDic:nil finish:^(NSData *data) {
        
        //  当是刷新的时候,删除之前数组中的数据
        if (_refreashType == RefreashTypeNew) {
            [self.dataArray removeAllObjects];
        }
        
        NSDictionary *parDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *arr = parDic[@"data"][@"items"];
        for (NSDictionary *dic in arr) {
            HomeGiftModel *model = [[HomeGiftModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [MBProgressHUD hideHUD];
        //  刷新UI界面
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}



#pragma mark --- UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HomeGiftCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    
    HomeGiftModel *model = _dataArray[indexPath.row];
    cell.giftModel = model;
    
    //  设置cell没有选中的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return (273 * (SCREEN_WIDTH - 20) / 600) + 20;
}



#pragma mark --- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    HomeBaseDetailViewController *homeVC = [[HomeBaseDetailViewController alloc] init];
    HomeGiftModel *model = _dataArray[indexPath.row];
    NSInteger num = [model.ID integerValue];
    homeVC.ID = [NSString stringWithFormat:@"%ld",(long)num];
    homeVC.naVC = self.nac;
    [self.nac pushViewController:homeVC animated:YES];
}



#pragma mark --- action
- (void)refreashData{

    [self.tableView addGifHeaderWithRefreshingTarget:self refreshingAction:@selector(refeashNew)];
    [self.tableView addGifFooterWithRefreshingTarget:self refreshingAction:@selector(refreashMore)];
}

//  刷新数据(重新开始加载)
- (void)refeashNew{
    
    _num = 0;
    _refreashType = RefreashTypeNew;
    [self readData];
    [self.tableView.gifHeader endRefreshing];
}

//  加载数据(加载一次,增加20条数据)
- (void)refreashMore{
    
    _num += 20;
    _refreashType = RefreashTypeMore;
    [self readData];
    [self.tableView.gifFooter endRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
